//
//  SEGNaviHeaderView.m
//  Commom
//
//  Created by 山刘 on 2018/11/21.
//  Copyright © 2018年 山刘. All rights reserved.
//

#import "SEGNaviHeaderView.h"

#if __has_include(<Commom/YYBaseHeader.h>)
#import <Commom/YYBaseHeader.h>
#else
#import "YYBaseHeader.h"
#endif

@interface SEGNaviHeaderView()
@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UIButton *closeBtn;

@property (nonatomic, strong) UILabel *titleLabel;
@end

@implementation SEGNaviHeaderView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    self.userInteractionEnabled = YES;
    self.backgroundColor = [UIColor clearColor];
    
    [self addSubview:self.backBtn];
    
    [self addSubview:self.shareBtn];
    [self addSubview:self.closeBtn];
    
    [self addSubview:self.titleLabel];
    [self addSubview:self.headerScrollView];
    
    [self setupUI];
    self.backgroundColor = self.superview.backgroundColor;
    return self;
}

//界面布局
-(void)setupUI{
}

-(void)layoutSubviews{
     [super layoutSubviews];
    
    self.backBtn.left = 15.0;
    [self.backBtn sizeToFit];
    self.backBtn.top = (self.height - self.backBtn.height)*0.5;
    self.backBtn.width += self.backBtn.width + 15;
    
    self.shareBtn.right = self.width - 15.0;
    [self.shareBtn sizeToFit];
    self.shareBtn.top = (self.height - self.shareBtn.height)*0.5;
    
    if (self.shareBtn.isHidden) {
        self.closeBtn.right = self.width - 15.0;
        [self.closeBtn sizeToFit];
        self.closeBtn.top = (self.height - self.closeBtn.height)*0.5;
    }else{
        self.closeBtn.right = CGRectGetMinX(self.shareBtn.frame) - 10;
        [self.closeBtn sizeToFit];
        self.closeBtn.top = (self.height - self.closeBtn.height)*0.5;
    }
    
    [self.titleLabel sizeToFit];
    self.titleLabel.width = self.titleLabel.width > ([UIScreen mainScreen].bounds.size.width * 0.5) ?  [UIScreen mainScreen].bounds.size.width * 0.5 : self.titleLabel.width;
    self.titleLabel.center = self.center;
    
    self.headerScrollView.frame = CGRectMake(0, 0, 0, 0);
    self.headerScrollView.width = [UIScreen mainScreen].bounds.size.width * 0.5;
    self.headerScrollView.height = self.height;
    self.headerScrollView.center = self.center;
}


/**
 展示分享按钮
 */
-(void)showShare{
    self.shareBtn.hidden = NO;
}

/**
 隐藏分享按钮
 */
-(void)hideShare{
    self.shareBtn.hidden = YES;
    if (self.closeBtn) {
        self.closeBtn.right = self.width - 15.0;
        [self.closeBtn sizeToFit];
        self.closeBtn.top = (self.height - self.closeBtn.height)*0.5;
    }
}

#pragma mark - getter

-(UIButton *)backBtn{
    if (!_backBtn) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _backBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_backBtn setImage:[UIImage segBundleImageNamed:@"icon_back"] forState:UIControlStateNormal];
        _backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _backBtn;
}

-(void)backBtnClick:(UIButton*)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(segbackBtnClick)]) {
        [self.delegate segbackBtnClick];
    }
}

-(UIButton *)shareBtn{
    if (!_shareBtn) {
        _shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _shareBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_shareBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        _shareBtn.hidden = YES;
        [_shareBtn setImage:[UIImage segBundleImageNamed:@"btn_header_share"] forState:UIControlStateNormal];
        [_shareBtn addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _shareBtn;
}

-(void)shareBtnClick:(UIButton*)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(shareBtnClick:)]) {
        [self.delegate shareBtnClick:self.data];
    }
}

-(UIButton *)closeBtn{
    if (!_closeBtn) {
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _closeBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_closeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_closeBtn setImage:[UIImage segBundleImageNamed:@"btn_dialog_close_gray"] forState:UIControlStateNormal];
        _closeBtn.hidden = YES;
        [_closeBtn addTarget:self action:@selector(closeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _closeBtn;
}

-(void)closeBtnClick:(UIButton*)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(segcloseBtnClick)]) {
        [self.delegate segcloseBtnClick];
    }
}

-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.userInteractionEnabled = NO;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.hidden = YES;
        _titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    }
    return _titleLabel;
}

-(SEGNaviHeaderScrollView *)headerScrollView{
    if (!_headerScrollView) {
        _headerScrollView = [[SEGNaviHeaderScrollView alloc]init];
        _headerScrollView.hidden = YES;
        _headerScrollView.backgroundColor = self.backgroundColor;
    }
    return _headerScrollView;
}

-(void)setTitle:(NSString *)title{
    _title = title;
    
    if (title && [title isKindOfClass:[NSString class]] && title.length > 0 && self.headerScrollView.hidden == YES) {
        self.titleLabel.text = title;
        self.titleLabel.hidden = NO;
    }else{
        self.titleLabel.hidden = YES;
        self.titleLabel.text = @"";
    }
    [self layoutSubviews];
}

-(void)setCanShowCloseButton:(BOOL)canShowCloseButton{
    _canShowCloseButton = canShowCloseButton;
    self.closeBtn.hidden = !canShowCloseButton;
}

- (void)setBackButtonImage:(UIImage *)backButtonImage {
    if (backButtonImage) {
        [self.backBtn setImage:backButtonImage forState:UIControlStateNormal];
        [self.backBtn sizeToFit];
    }
}

@end
